package com.taoshen.controller;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import cn.hutool.extra.servlet.ServletUtil;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Description
 * @Author Zhang Litao
 * @Date 2021/12/15 13:36
 */
@RestController
@RequestMapping("qrCode")
public class QrCodeController {
    @RequestMapping("get")
    public void getQrCode(HttpServletResponse response) throws IOException {
        // 获取输出流
        ServletOutputStream outputStream = response.getOutputStream();

        // 设置生成二维码图片的配置信息
        QrConfig qrConfig = new QrConfig();
        qrConfig.setHeight(500);
        qrConfig.setWidth(500);
        qrConfig.setErrorCorrection(ErrorCorrectionLevel.H);

        // 生成二维码
        QrCodeUtil.generate("http://121.40.138.6:8080/datav/getAreaInfo?value=120101",
                qrConfig,
                ImgUtil.IMAGE_TYPE_JPG,
                outputStream);

        // 设置response响应头
        response.setHeader("content-disposition", "attachment;filename=test.jpg");
        outputStream.close();
    }

    @GetMapping("test")
    public void test(HttpServletRequest request) {
        // 获取客户端的ip地址
        String clientIP = ServletUtil.getClientIP(request);
        Map<String, String> paramMap = ServletUtil.getParamMap(request);
        paramMap.forEach((k, v) -> System.out.println(k + ":" + v));
    }


}
